import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_all.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/tab_home/xindong_search_yemian.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_tab_indicator.dart';
import 'package:xindong/widgets/container/xindong_home_container.dart';
import 'package:xindong/widgets/dialog/xindong_alert_message.dart';

import 'controller/xindong_home_tab_controller.dart';

class XinDongHomeYeMian extends StatefulWidget {
  const XinDongHomeYeMian({super.key});

  @override
  State<XinDongHomeYeMian> createState() => _XinDongHomeYeMianState();
}

class _XinDongHomeYeMianState extends State<XinDongHomeYeMian> with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  final _tabController = Get.put(XinDongHomeTabController());

  void onFilter() {
    // 暂停所有播放器播放
    XinDongAll.instance.pauseAllPlayer();
    XinDongPageSingleton.instance.pushYeMian(const XinDongSearchYeMian());
  }

  @override
  void initState() {
    super.initState();

    // Tab控制器
    _tabController.initWithVsync(this);
  }

  // 避免状态重置
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return XinDongHomeBackgroundView(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: XinDongAppBar(
          "",
          leading: const SizedBox(),
          leadingWidth: 0.0,
          elevation: 0.0,
          backgroundColor: Colors.transparent,
          titleView: Row(
            children: [
              Expanded(
                child: TabBar(
                  isScrollable: true,
                  indicator: const XinDongTabIndicator(color: DZColors.mainColor),
                  indicatorSize: TabBarIndicatorSize.label,
                  labelPadding: const EdgeInsets.only(left: 16, right: 5),
                  labelColor: DZColors.titleColor,
                  unselectedLabelColor: DZColors.placeholderColor,
                  labelStyle: const TextStyle(fontSize: kTitleFontSize, fontWeight: FontWeight.w500),
                  unselectedLabelStyle: const TextStyle(fontSize: 16),
                  tabs: _tabController.tabBar(),
                  controller: _tabController.tabController,
                  onTap: (index) {
                    // 附近的人需要会员
                    if (!XinDongUserController.to.isNoble.value && index == 1) {
                      _tabController.tabController.animateTo(0, duration: Duration.zero);
                      XinDongAlertMessage.show(
                        content: "查看附近的用户需要会员权限",
                        button: "立即开通",
                        onTapButton: () {
                          XinDongPageSingleton.instance.openNoble();
                        },
                      );
                    }
                  },
                ),
              ),
              GestureDetector(
                onTap: onFilter,
                behavior: HitTestBehavior.opaque,
                child: Padding(
                  padding: const EdgeInsets.only(right: kPaddingValue, top: 8, bottom: 8),
                  child: Container(
                    height: 28,
                    padding: const EdgeInsets.only(left: 8, right: 8),
                    decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(20)),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        ImageUtil.asset(AssetsRes.ICON_FILTER, width: 20, height: 20),
                        const Text(
                          "筛选",
                          style: TextStyle(fontSize: 15, color: DZColors.titleColor, height: 1.2),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
        body: Obx(
          () => TabBarView(
            controller: _tabController.tabController,
            physics: XinDongUserController.to.isNoble.value ? null : const NeverScrollableScrollPhysics(),
            children: _tabController.tabBarViews(),
          ),
        ),
      ),
    );
  }
}
